<?php

declare(strict_types=1);

namespace App\CustHub\Model;

use App\Common\Constants\PermissionTypeConstants;
use App\Common\Model\CommonRole;
use App\CustHub\Policies\ModelPolicy;
use App\System\Model\SystemRole;
use Hyperf\Database\Model\Builder;
use Hyperf\Database\Model\Relations\BelongsToMany;
use Hyperf\Database\Model\SoftDeletes;
use Hyperf\Di\Annotation\Inject;
use Mine\MineModel;

/**
 * @property int $id 
 * @property string $username 姓名
 * @property string $account 账号
 * @property string $nickname 微信昵称
 * @property string $openid 微信openid
 * @property string $unionid 
 * @property string $avatar 头像
 * @property string $mobile 手机号
 * @property int $parent_id 父级ID: 0=代表主账号
 * @property int $gender 性别，0=保密，1=男，2=女
 * @property int $status 状态 (1正常 2停用)
 * @property string $joinip 注册ip地址
 * @property string $prev_time 上次登录的时间
 * @property string $login_ip 最后登陆IP
 * @property string $login_time 最后登陆时间
 * @property string $invite_code 邀请码
 * @property string $money 用户余额
 * @property string $total_recharge 累计充值金额
 * @property int $invite_id 邀请人ID
 * @property int $seat_count 基础账号席位数量
 * @property int $corp_seat_count 互通账号席位数量
 * @property int $created_by 创建者
 * @property int $updated_by 更新者
 * @property \Carbon\Carbon $created_at 创建时间
 * @property \Carbon\Carbon $updated_at 更新时间
 * @property string $deleted_at 删除时间
 * @property string $remark 备注
 * @property-write mixed $password 密码
 * @property-read null|ChUserExt $ext 
 * @property-read null|\Hyperf\Database\Model\Collection|CommonRole[] $roles 
 */
class ChUser extends MineModel
{
    #[Inject]
    protected ModelPolicy $modelPolicy;

    use SoftDeletes;

    /**
     * 状态
     */
    public const ENABLE = 1;
    public const DISABLE = 0;

    /**
     * The table associated with the model.
     */
    protected ?string $table = 'ch_user';

    /**
     * The attributes that are mass assignable.
     */
    protected array $fillable = ['id', 'username', 'account', 'nickname', 'openid', 'unionid', 'avatar', 'mobile', 'password', 'parent_id', 'gender', 'status', 'joinip', 'prev_time', 'login_ip', 'login_time', 'invite_code', 'money', 'total_recharge', 'invite_id', 'seat_count', 'corp_seat_count', 'created_by', 'updated_by', 'created_at', 'updated_at', 'deleted_at', 'remark'];

    /**
     * The attributes that should be cast to native types.
     */
    protected array $casts = ['id' => 'integer', 'parent_id' => 'integer', 'gender' => 'integer', 'status' => 'integer', 'money' => 'decimal:2', 'total_recharge' => 'decimal:2', 'invite_id' => 'integer', 'seat_count' => 'integer', 'corp_seat_count' => 'integer', 'created_by' => 'integer', 'updated_by' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime'];

    /**
     * 密码加密.
     * @param mixed $value
     */
    public function setPasswordAttribute($value): void
    {
        $this->attributes['password'] = password_hash($value, PASSWORD_DEFAULT);
    }

    /**
     * 验证密码
     * @param mixed $password
     * @param mixed $hash
     */
    public static function passwordVerify($password, $hash): bool
    {
        return password_verify($password, $hash);
    }

    /**
     * 用户额外参数
     * @return \Hyperf\Database\Model\Relations\HasOne
     */
    public function ext(): \Hyperf\Database\Model\Relations\HasOne
    {
        return $this->hasOne(ChUserExt::class, 'uid', 'id');
    }


    /**
     * 通过中间表关联角色.
     */
    public function roles(): BelongsToMany
    {
        return $this->belongsToMany(CommonRole::class, 'common_user_role', 'user_id', 'role_id')
            ->withPivot(['permission_type','employee_id'])
            ->wherePivot('permission_type','=',PermissionTypeConstants::CUST_HUB)
            ->wherePivot('employee_id','=',0);
    }

    /**
     * 数据权限过滤
     * @param $query
     * @param int|null $uid
     * @return Builder
     */
    public function scopeWithPermission($query, ?int $uid = null): Builder
    {
        return $this->modelPolicy->apply($query, $this, $uid);
    }
}
